/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import { message, Modal } from 'antd';
import { getList, specialAdd, specialDel } from '@/services/fastMsg';

export default {
  namespace: 'fastMsg',

  state: {
    list: [],
    pagination: null,
    visible: false,
  },

  effects: {
    *getList({ payload }, { put, call }) {
      const { download } = payload;
      const response = yield call(getList, payload);
      const { code, data, msg } = response;
      if (code == 0) {
        if (download) {
          Modal.info({
            title: '温馨提示',
            content: '导出数据申请已提交，请至【报表下载】处下载',
          });
        } else if (data) {
          const { list, total, page } = data;
          yield put({
            type: 'save',
            payload: {
              list,
              pagination: {
                total,
                current: page,
                sizeType: false,
                showQuickJumper: false,
                pageSize: 15,
              },
            },
          });
        }
      } else {
        message.warning(msg);
      }
    },
    *specialAdd({ payload, then }, { call }) {
      // 快件录入
      const response = yield call(specialAdd, payload);
      then(response);
    },
    *specialDel({ payload, then }, { put, call }) {
      const response = yield call(specialDel, payload);
      const { code, msg, data } = response;
      if (code == 0 && data) {
        message.success(msg);
        yield put({
          type: 'save',
          payload: {
            visible: false,
          },
        });
        then(response);
      } else {
        message.error(msg);
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
